package cn.edu.cqvie.netty.http;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

import java.net.URI;


/**
 * http 处理器 (处理请求进入)
 *
 * @author zhengsh
 */
public class TestHttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {


    /**
     * 读取客户端数据，响应客户端数据
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {

//        class io.netty.handler.codec.http.DefaultHttpRequest
//        class io.netty.handler.codec.http.LastHttpContent$1

        System.out.println(msg.getClass());

        System.out.println(ctx.channel().remoteAddress());

        Thread.sleep(8000);

        if (msg instanceof HttpRequest) {

            //获取请求
            HttpRequest httpRequest = (HttpRequest) msg;

            System.out.println("请求方法名：" + httpRequest.method());
            // 获取URI，做简单的路由
            URI uri = new URI(httpRequest.uri());
            if("/favicon.ico".equals(uri.getPath())) {
                System.out.println("请求 favicon.ico");
                return;
            }

            System.out.println("执行 channelRead0");
            //返回内容
            ByteBuf content = Unpooled.copiedBuffer("Hello World", CharsetUtil.UTF_8);

            //构建响应
            FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                    HttpResponseStatus.OK,
                    content);
            //请求头，内容类型
            response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
            //请求头，内容长度
            response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());

            //响应数据
            ctx.writeAndFlush(response);
            ctx.channel().close();
        }
    }


    /**
     * 活动状态
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(" channel Active");
        super.channelActive(ctx);
    }

    /**
     * 注册连接
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println(" channel Registered");

        super.channelRegistered(ctx);
    }

    /**
     * 添加处理器
     * @param ctx
     * @throws Exception
     */
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        System.out.println(" handler Added");

        super.handlerAdded(ctx);
    }

    /**
     * 不活动状态
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println(" channel Inactive");

        super.channelInactive(ctx);
    }

    /**
     * 解除注册
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println(" channel Unregistered");

        super.channelUnregistered(ctx);
    }
}
